package com.cx.auth.controller;

import com.cx.auth.enums.ResponseCodeEnum;
import com.cx.auth.model.vo.ResultBean;
import com.cx.auth.model.vo.UserDetailsVO;
import com.cx.auth.vo.UserDetailsWrap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.web.bind.annotation.*;

/**
 * @author: zhangcx
 * @Description: 鉴权controller
 * @date: 2024/1/3 8:01
 * @email: zhangcx@bluemoon.com.cn
 */

@CrossOrigin
@RestController
@RequestMapping("/oauth")
public class OauthController {

    /**
     * 授权码登录模式-回调url地址接口
     * @param code
     * @return
     */
    @GetMapping("/code")
    public String code(@RequestParam(value = "code",required = false)String code){
        return code;
    }

    @Autowired
    @Qualifier("commonTokenStore")
    private TokenStore commonTokenStore;

    @Autowired
    @Qualifier("smsTokenStore")
    private TokenStore smsTokenStore;

    /**
     * 登出接口
     * @param token
     * @return
     */
    @PostMapping("/logout")
    public ResultBean delToken(@RequestParam("token") String token) {
        OAuth2AccessToken accessToken = commonTokenStore.readAccessToken(token);

        if (accessToken != null && !StringUtils.isBlank(accessToken.getValue())) {
            // 清空access token
            commonTokenStore.removeAccessToken(accessToken);

            // 清空 refresh token
            OAuth2RefreshToken refreshToken = accessToken.getRefreshToken();
            commonTokenStore.removeRefreshToken(refreshToken);
        }

        OAuth2AccessToken smsToken = smsTokenStore.readAccessToken(token);
        if (smsToken != null && !StringUtils.isBlank(smsToken.getValue())) {
            // 清空access token
            smsTokenStore.removeAccessToken(smsToken);

            // 清空 refresh token
            OAuth2RefreshToken refreshToken = smsToken.getRefreshToken();
            smsTokenStore.removeRefreshToken(refreshToken);
        }
        return ResultBean.createSuccess();
    }

    /**
     * 根据token获取用户信息
     * @param token
     * @return
     */
    @PostMapping("getUserInfo")
    public ResultBean<UserDetailsVO> getUserInfo(@RequestParam("token") String token){
        UserDetailsVO userDetailsVO= null;
        ResultBean responseBean = ResultBean.createSuccess();

        OAuth2AccessToken accessToken = commonTokenStore.readAccessToken(token);
        OAuth2AccessToken smsToken = smsTokenStore.readAccessToken(token);

        //校验token
        if (accessToken == null && smsToken == null){
            return responseBean.createFail(ResponseCodeEnum.TOKEN_FAILURE_ERR.getCode(),ResponseCodeEnum.TOKEN_FAILURE_ERR.getFMsg());
        }

        if (accessToken != null && !StringUtils.isBlank(accessToken.getValue())) {
            OAuth2Authentication auth2Authentication = commonTokenStore.readAuthentication(token);
            UserDetailsWrap userDetails = (UserDetailsWrap) auth2Authentication.getUserAuthentication().getPrincipal();
            if (userDetails != null && userDetails.getUser() != null){
                userDetailsVO = new UserDetailsVO();
                BeanUtils.copyProperties(userDetails.getUser(), userDetailsVO);
            }
        }


        if (smsToken != null && !StringUtils.isBlank(smsToken.getValue())) {
            OAuth2Authentication auth2Authentication = smsTokenStore.readAuthentication(token);
            UserDetailsWrap userDetails = (UserDetailsWrap) auth2Authentication.getUserAuthentication().getPrincipal();
            if (userDetails != null && userDetails.getUser() != null){
                userDetailsVO = new UserDetailsVO();
                BeanUtils.copyProperties(userDetails.getUser(), userDetailsVO);
            }
        }

        if (userDetailsVO == null){
            return responseBean.createFail(ResponseCodeEnum.TOKEN_USER_ERR.getCode(),ResponseCodeEnum.TOKEN_USER_ERR.getFMsg());
        }


        responseBean.setData(userDetailsVO);
        return responseBean;
    }
}
